package com.hqd.jjvm.util;

import org.apache.commons.lang.StringUtils;

/**
 * 十六进制字符串转换器
 *
 * @author hqd
 */
public class HexStrTransformUtil {
    public static Double parseHexToDouble(String hexStr) {
        if (StringUtils.isNotBlank(hexStr)) {
            return Double.longBitsToDouble(parseHexToLong(hexStr));
        }
        return null;
    }

    public static Long parseHexToLong(String hexStr) {
        if (StringUtils.isNotBlank(hexStr)) {
            return Long.parseUnsignedLong(hexStr, 16);
        }
        return null;
    }

    public static Integer parseHexToInt(String hexStr) {
        if (StringUtils.isNotBlank(hexStr)) {
            return Integer.parseUnsignedInt(hexStr, 16);
        }
        return null;
    }

    public static Float parseHexToFloat(String hexStr) {
        if (StringUtils.isNotBlank(hexStr)) {
            return Float.valueOf(parseHexToInt(hexStr));
        }
        return null;
    }

    public static String pareHexToStr(String hexStr) {
        if (StringUtils.isNotBlank(hexStr)) {
            byte[] bytes = new byte[hexStr.length()];
            for (int i = 0; i < hexStr.length() >> 1; i++) {
                bytes[i] = (byte) (0xff & Integer.parseInt(hexStr.substring(i * 2, i * 2 + 2), 16));
            }
            return new String(bytes).trim();
        }
        return "";
    }
}
